package com.atguigu.guli.service.edu.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.Subject;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.excel.ExcelCourseData;
import com.atguigu.guli.service.edu.mapper.CourseDescriptionMapper;
import com.atguigu.guli.service.edu.mapper.CourseMapper;
import com.atguigu.guli.service.edu.mapper.SubjectMapper;
import com.atguigu.guli.service.edu.mapper.TeacherMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Random;

/**
 * @author lijiatao
 * @since 2020/4/18
 */

@Slf4j
@NoArgsConstructor //无参构造函数
@AllArgsConstructor //全参构造函数
public class ExcelCourseDataListener extends AnalysisEventListener<ExcelCourseData> {

    private SubjectMapper subjectMapper;
    private CourseMapper courseMapper;
    private TeacherMapper teacherMapper;
    private CourseDescriptionMapper courseDescriptionMapper;

    /**
     *遍历每一行的记录
     * @param data
     * @param context
     */
    @Override
    public void invoke(ExcelCourseData data, AnalysisContext context) {
        log.info("解析到一条记录: {}", data);
        //处理读取出来的数据
        String teacherName = data.getTeacherName();
        String subjectParentName = data.getSubjectParentName();
        String subjectSonName = data.getSubjectSonName();
        String title = data.getTitle();
        String lessonNum1 = data.getLessonNum();
        String lessonNum = lessonNum1.substring(0,lessonNum1.indexOf("."));
        String description = data.getDescription();
        String cover = data.getCover();
        log.info("teacherName: {}", teacherName);
        log.info("subjectParentName: {}", subjectParentName);
        log.info("subjectSonName: {}", subjectSonName);
        log.info("title: {}", title);
        log.info("lessonNum: {}", lessonNum);
        log.info("description: {}", description);
        log.info("cover: {}", cover);


        //判断一级分类数据是否存在
        Subject subjectLevelOne = this.getByTitle(subjectParentName);
        String parentId = null;
        if(subjectLevelOne == null){
            //组装一级类别
            Subject subject = new Subject();
            subject.setParentId("0");
            subject.setTitle(subjectParentName);
            // 存入数据库
            subjectMapper.insert(subject);
            parentId = subject.getId();
        }else{
            parentId = subjectLevelOne.getId();
        }

        //判断二级分类数据是否存在
        Subject subjectLevelTwo = this.getSubByTitle(subjectSonName, parentId);
        String subjectSonId = null;
        if(subjectLevelTwo == null) {
            //组装二级类别
            Subject subject = new Subject();
            subject.setTitle(subjectSonName);
            subject.setParentId(parentId);
            // 存入数据库
            subjectMapper.insert(subject);

//            subjectSonId = this.getSubByTitle(subjectSonName,parentId).getId();
            subjectSonId = subject.getId();
        }else {
            subjectSonId = subjectLevelTwo.getId();
        }

        Course course = new Course();
        String teacherId = getTeacherByName(teacherName).getId();
        if (teacherId == null){
            log.info("该教师名字不存在: {}", teacherName);
            teacherId = "1";
        }
        //随机生成1-10之间的数
        Random random = new Random();
        int j = random.nextInt(10);
        teacherId = String.valueOf(j);

//        course.setId(String.valueOf(System.currentTimeMillis()));
        course.setTeacherId(teacherId);
        course.setSubjectParentId(parentId);
        course.setSubjectId(subjectSonId);
        course.setTitle(title);
        course.setLessonNum(Integer.valueOf(lessonNum));
        course.setCover(cover);
        course.setStatus(Course.COURSE_NORMAL);
        course.setGmtCreate(new Date());
        course.setGmtModified(new Date());
        courseMapper.insert(course);

        //保存CourseDescription
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(description);
        courseDescription.setId(course.getId());
        courseDescriptionMapper.insert(courseDescription);


    }

    /**
     * 所有数据读取之后的收尾工作
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("全部数据解析完成");
    }

    /**
     * 根据教师名称查询数据是否存在
     * @return
     */
    private Teacher getTeacherByName(String teacherName){
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", teacherName);
        return teacherMapper.selectOne(queryWrapper);
    }


    /**
     * 根据一级分类的名称查询数据是否存在
     * @param title
     * @return
     */
    private Subject getByTitle(String title){
        QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title", title);
        queryWrapper.eq("parent_id", "0"); //一级分类
        return subjectMapper.selectOne(queryWrapper);
    }


    /**
     * 根据分类的名称和父id查询数据是否存在
     * @param title
     * @param parentId
     * @return
     */
    private Subject getSubByTitle(String title, String parentId){
        QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title", title);
        queryWrapper.eq("parent_id", parentId); //二级分类
        return subjectMapper.selectOne(queryWrapper);
    }

}
